﻿Imports System
Imports System.CodeDom.Compiler
Imports System.Collections
Imports System.Globalization
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel.Compiler
    <Serializable> _
    Public NotInheritable Class WorkflowCompilerError
        Inherits CompilerError
        Implements IWorkflowCompilerError
        ' Methods
        Public Sub New()
        End Sub

        Friend Sub New(ByVal [error] As CompilerError)
            If ([error] Is Nothing) Then
                Throw New ArgumentNullException("error")
            End If
            MyBase.Column = ([error].Column - 1)
            MyBase.ErrorNumber = [error].ErrorNumber
            MyBase.ErrorText = [error].ErrorText
            MyBase.FileName = [error].FileName
            MyBase.IsWarning = [error].IsWarning
            MyBase.Line = ([error].Line - 1)
            Me.incrementLineAndColumn = True
        End Sub

        Public Sub New(ByVal fileName As String, ByVal exception As WorkflowMarkupSerializationException)
            If (exception Is Nothing) Then
                Throw New ArgumentNullException("exception")
            End If
            MyBase.FileName = fileName
            MyBase.Line = (exception.LineNumber - 1)
            MyBase.Column = (exception.LinePosition - 1)
            MyBase.ErrorText = exception.Message
            MyBase.ErrorNumber = &H15B.ToString(CultureInfo.InvariantCulture)
            Me.incrementLineAndColumn = True
        End Sub

        Public Sub New(ByVal fileName As String, ByVal line As Integer, ByVal column As Integer, ByVal errorNumber As String, ByVal errorText As String)
            MyBase.New(fileName, line, column, errorNumber, errorText)
        End Sub

        Public Overrides Function ToString() As String
            If (MyBase.FileName.Length > 0) Then
                If ((MyBase.Line <= 0) OrElse (MyBase.Column <= 0)) Then
                    Return String.Format(CultureInfo.CurrentCulture, "{0} : {1} {2}: {3}", New Object() { MyBase.FileName, If(MyBase.IsWarning, "warning", "error"), MyBase.ErrorNumber, MyBase.ErrorText })
                End If
                Return String.Format(CultureInfo.CurrentCulture, "{0}({1},{2}) : {3} {4}: {5}", New Object() { MyBase.FileName, If(Me.incrementLineAndColumn, (MyBase.Line + 1), MyBase.Line), If(Me.incrementLineAndColumn, (MyBase.Column + 1), MyBase.Column), If(MyBase.IsWarning, "warning", "error"), MyBase.ErrorNumber, MyBase.ErrorText })
            End If
            Return String.Format(CultureInfo.CurrentCulture, "{0} {1}: {2}", New Object() { If(MyBase.IsWarning, "warning", "error"), MyBase.ErrorNumber, MyBase.ErrorText })
        End Function


        ' Properties
        Public Property PropertyName As String
            Get
                Return Me.propertyName
            End Get
            Set(ByVal value As String)
                Me.propertyName = value
            End Set
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.ColumnNumber As Integer
            Get
                Return MyBase.Column
            End Get
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.Document As String
            Get
                Return MyBase.FileName
            End Get
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.ErrorNumber As String
            Get
                Return MyBase.ErrorNumber
            End Get
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.IsWarning As Boolean
            Get
                Return MyBase.IsWarning
            End Get
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.LineNumber As Integer
            Get
                Return MyBase.Line
            End Get
        End Property

        Private ReadOnly Property System.Workflow.ComponentModel.Compiler.IWorkflowCompilerError.Text As String
            Get
                Return MyBase.ErrorText
            End Get
        End Property

        Public ReadOnly Property UserData As IDictionary
            Get
                If (Me.userData Is Nothing) Then
                    Me.userData = New Hashtable
                End If
                Return Me.userData
            End Get
        End Property


        ' Fields
        Private incrementLineAndColumn As Boolean
        Private propertyName As String
        Private userData As Hashtable
    End Class
End Namespace

